<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use common\models\User;
use common\models\KnowledgeModel;
use common\models\ResourceModel;
use common\models\ResrelationknowModel;
use yii\data\Pagination;

class UserController extends Controller
{
    //个人中心
    public function actionIndex(){
        $this->layout="layout3.php";
        $model=new User();
        $data=$model->find()->where(['userID'=>'xinlan'])->asArray()->one();
        return $this->render('index',['data'=>$data]);
    }
    
    public function actionKnowmap(){
        $this->layout="layout3.php";
        $model=new \common\models\KnowmapModel();
        $data=$model->find()->where(['userID'=>'xinlan'])->asArray()->all();
        return $this->render('knowmap',['data'=>$data]);
    }
    
    public function actionLeft(){
        return $this->render('left');
    }
    
    //知识点管理index
    public function actionKnowManager(){
        $this->layout="layout3.php";
        $model=new KnowledgeModel();
        $obj=$model->find();
        $count=$obj->count();
        $pageSize=7;
        $pager=new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);
        $data=$obj->offset($pager->offset)->limit($pager->limit)->all();
        if(Yii::$app->request->isPost){
            $post=Yii::$app->request->post();
            $searchData=$model->getSearch($post,$pageSize);//最后返回的是数组
            return $this->render('knowmanager',['data'=>$searchData['data'],'pager'=>$searchData['pager'],'model'=>$model]);
        }else{
        //，没有用array，传递的是对象
        return $this->render('knowmanager',['data'=>$data,'pager'=>$pager,'model'=>$model]);
        }
    }
    
    //知识点删除
    public function actionDelknow()
    {
        $this->layout="layout3.php";
        $id= (int)\Yii::$app->request->get('id');
        if(empty($id)){
            $this->redirect(['user/know-manager']);
        }
        $model=new KnowledgeModel();
        if($model->deleteAll('id=:id',[':id'=>$id])){
            \Yii::$app->session->setFlash('info','删除成功');
            $this->redirect(['user/know-manager']);
        }
    }
    
    //知识点搜索
    public function actionSearchknow()
    {
        
    }
    
    //编辑知识点
    public function actionEditknow()
    {
        $this->layout="layout3.php";
        $id= (int)Yii::$app->request->get('id');
        if(empty($id)){
            $this->redirect(['user/know-manager']);
        }
        $model=new KnowledgeModel();
        $data=$model->getknowbyid($id);
        if(Yii::$app->request->isPost){
            $post= Yii::$app->request->post();
            if($model->editknow($post,$id)){
                $this->redirect(['user/know-manager']);
                Yii::$app->session->setFlash('info','修改成功');
            }
        }
        return $this->render('editknow',['data'=>$data]);
    }

    //添加知识点
    public function actionAddknow()
    {
        $this->layout="layout3.php";
        $model=new KnowledgeModel;
        if(\Yii::$app->request->isPost){
            $post= \Yii::$app->request->post();
            if($model->addknowway($post)){
                $this->redirect(['user/know-manager']);
            }
        }
        return $this->render('addknow',['data'=>$model]);
    }

    //导入资源
    public function actionUploadres()
    {
        $this->layout="layout3.php";
        return $this->render('uploadres');
    }
    //导入资源文件
    public function actionUploadfile()
    {
        $this->layout="layout3.php";
        return $this->render('uploadfile');
    }
    
    //资源管理
    public function actionResmanager()
    {
        $this->layout="layout3.php";
        $model=new ResourceModel();
        $obj=$model->find();
        $count=$obj->count();
        $pageSize=7;
        $pager=new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);
        $data=$obj->offset($pager->offset)->limit($pager->limit)->all();
        if(Yii::$app->request->isPost){
            $post=Yii::$app->request->post();
            $searchData=$model->getSearch($post,$pageSize);//最后返回的是数组
            return $this->render('resmanager',['data'=>$searchData['data'],'pager'=>$searchData['pager'],'model'=>$model]);
        }else{
        //，没有用array，传递的是对象
        return $this->render('resmanager',['data'=>$data,'pager'=>$pager,'model'=>$model]);
        }
    }
    
    //资源信息
    public function actionResourceinfo()
    {
        $this->layout="layout3.php";
        $id= (int)Yii::$app->request->get('id');
        if(empty($id)){
            $this->redirect(['user/resnamager']);
        }
        $model=new ResourceModel();
        $data=$model->getknowbyid($id);
        if(Yii::$app->request->isPost){
            $post= Yii::$app->request->post();
            if($model->editknow($post,$id)){
                $this->redirect(['user/resnamager']);
                Yii::$app->session->setFlash('info','修改成功');
            }
        }
        return $this->render('resourceinfo',['data'=>$data]);
    }

    public function actionDelres()
    {
        $this->layout="layout3.php";
        $id= (int)\Yii::$app->request->get('id');
        if(empty($id)){
            $this->redirect(['user/resmanager']);
        }
        $model=new ResourceModel();
        if($model->deleteAll('id=:id',[':id'=>$id])){
            \Yii::$app->session->setFlash('info','删除成功');
            $this->redirect(['user/resmanager']);
        }
    }
    
    //资源关联管理
    public function actionResRelamanager()
    {
        $this->layout="layout3.php";
        $model=new KnowledgeModel();
        $obj=$model->find();
        $count=$obj->count();
        $pageSize=7;
        $pager=new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);
        $data=$obj->offset($pager->offset)->limit($pager->limit)->all();
        if(Yii::$app->request->isPost){
            $post=Yii::$app->request->post();
            $searchData=$model->getSearch($post,$pageSize);//最后返回的是数组
            return $this->render('resrelamanager',['data'=>$searchData['data'],'pager'=>$searchData['pager'],'model'=>$model]);
        }else{
        //，没有用array，传递的是对象
        return $this->render('resrelamanager',['data'=>$data,'pager'=>$pager,'model'=>$model]);
        }
    }
    //知识点与资源关联
    public function actionResrelationcheck()
    {
        $this->layout="layout3.php";
        $knowid= Yii::$app->request->get('knowid');
        $model=new ResrelationknowModel();
        $obj=$model->find()->where('knowid=:kid',[':kid'=>$knowid])->joinWith('resource');
        $count=$obj->count();
        $pageSize=7;
        $pager=new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);
        $data=$obj->offset($pager->offset)->limit($pager->limit)->all();
        //，没有用array，传递的是对象
        return $this->render('resrelationcheck',['data'=>$data,'pager'=>$pager,'model'=>$model,'knowid'=>$knowid]);
    }
    
    //修改权重
    public function actionWeightchange()
    {
            $post= Yii::$app->request->post();
            $wid=$post('id');
            $weight=$post('weight');
            $data=ResrelationknowModel::updateAll(['weight'=>$weight],'id=:wid',[':wid'=>$wid]);
            if($data){
                $response=array(
                    'errno'=>0,
                    'errmsg'=>'success',
                    'data'=>true,
                );
            }else{
                   $response=array(
                    'errno'=>0,
                    'errmsg'=>'success',
                    'data'=>true,
                );
            }
            echo json_encode($response);
    }

    //单个知识点资源关联管理UI
    public function actionKnowResMan()
    {
        $this->layout="layout3.php";
        return $this->render('knowresman');
    }
    
    //分类列表
    public function actionCategoryman()
    {
        $this->layout="layout3.php";
        return $this->render('categoryman');
    }
    //添加新分类
    public function actionCategoryadd()
    {
        $this->layout="layout3.php";
        return $this->render('categoryadd');
    }
}
